<!-- 生成链接 -->
<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row :gutter="48">
                    <a-col :md="8" :sm="24">
                        <a-form-item label="文件名称">
                            <a-input v-model="remark" allow-clear placeholder="请输入文件名称" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item >
                            <a-button style="margin-right: 20px" type="primary" @click="getData()">查询</a-button>
                            <a-button icon="plus" @click="add()">新增</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>
        <a-table rowKey="id" :loading="isload" @change="handleTableChange" :columns="columns" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size}" :data-source="dataList"  size="small">
            <template slot="operation" slot-scope="text, record">
                <div class="editable-row-operations">
                    <a-button type="link" @click="update(record)">修改</a-button>
                    <a-popconfirm
                        title="是否确认删除当前数据?"
                        ok-text="Yes"
                        cancel-text="No"
                        @confirm="del(record)"
                    >
                        <a-button type="link">删除</a-button>
                    </a-popconfirm>
                </div>
            </template>
        </a-table>

        <a-modal
            title="添加、修改文件信息"
            :visible="visible"
            @ok="handleOk"
            :confirm-loading="confirmLoading"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="文件名称">
                        <a-input v-model="item.remark" :disabled="!isAdd" placeholder="请输入文件名称" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="文件">
                        <a-upload-dragger
                            name="file"
                            :file-list="fileList"
                            :before-upload="beforeUpload"
                        >
                            <p class="ant-upload-drag-icon">
                            <a-icon type="inbox" />
                            </p>
                            <p class="ant-upload-text">
                                拖拽或者点击上传文件
                            </p>
                        </a-upload-dragger>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { queryBrandUploadFiles , uploadBrandFile } from '@/api/power';
import { columns } from './table';
import { queryBrand }  from '@/api/brand';
export default {
    data () {
        return {
            remark: '',
            brandId: this.global.brandId,
            brandList: [],
            fileList: [],
            visible: false,
            isAdd: true,
            item: {
                id: '',
                remark: '',
            },
            dataList: [],
            page: 1,
            size: 10,
            total: 0,
            columns,
            isload: false,
            confirmLoading: false,
        };
    },
    created(){
        // this.getBrand()
        this.getData()
    },
    methods: {
        getBrand(){
            let _this = this
            let page = 0
            const fn = function(resolve){
                const params = {
                    'size':99,
                    'page':page,
                }
                queryBrand(params)
                .then(res=>{
                    if(res.success){
                        if(page == 0){
                            _this.brandList = res.data.content
                        }else{
                            _this.brandList = [..._this.brandList,...res.data.content]
                        }
                        if((res.data.totalElements > (page + 1) * 99)){
                            page++;
                            fn(resolve)
                        }else{
                            resolve()
                        }
                    }else{
                        _this.$notification['error']({
                            message: '提示',
                            description: res.errMessage,
                            duration: 8,
                        })
                    }
                })
            }
            return new Promise((resolve,rej)=>{
                fn(resolve)
            })
        },
        beforeUpload(file) {
            this.fileList = [file];
            return false;
        },
        getData(){
            let _this = this
            _this.isload = true
            const params = {
                'brandId':_this.brandId,
                'remark':_this.remark,
                'page':_this.page - 1,
                'size':_this.size,
            }
            queryBrandUploadFiles(params)
            .then(res=>{
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements
                    _this.isload = false
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 添加
        add(){
            if(!this.brandId){
                this.$message.warning('请先选择品牌！')
                return
            }
            this.item.id = ''
            this.item.brandId = this.brandId
            this.item.remark = ''
            this.fileList = []
            this.isAdd = true
            this.visible = true
        },
        // 修改
        update(item){
            this.item = JSON.parse(JSON.stringify(item))
            this.isAdd = false
            this.visible = true
        },
        // 确定添加、修改配置
        handleOk(){
            let _this = this
            if(_this.item.remark == ''){
                _this.$message.warning('请输入文件名称')
                return
            }
            _this.confirmLoading = true
            let formData = new FormData()
            formData.append('id',_this.item.id)
            formData.append('brandId',_this.item.brandId)
            formData.append('remark',_this.item.remark)
            formData.append('file',_this.fileList[0])
            uploadBrandFile(formData)
            .then(res=>{
                if(res.success){
                    _this.getData()
                    _this.visible = false
                    _this.$notification['success']({
                        message: '提示',
                        description: !_this.isAdd ? '修改成功' : '添加成功',
                        duration: 8,
                    })
                    _this.confirmLoading = false
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除
        del(item){
            let _this = this
            let formData = new FormData()
            formData.append('id',item.id)
            formData.append('isDelete',1)
            uploadBrandFile(formData)
            .then(res=>{
                if(res.success){
                    _this.getData()
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>